package com.wurao.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.wurao.entity.Blog;
import com.wurao.entity.PageBean;
import com.wurao.service.BlogService;
import com.wurao.util.PageUtil;
import com.wurao.util.StringUtil;

/**
 * 博客主页controller
 *
 * @author xxxx
 *
 */
@Controller
@RequestMapping("/")
public class IndexController {
	@Resource
	private BlogService blogService;

	/**
	 * 首页
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/home")
	public ModelAndView list(){
		ModelAndView mav=new ModelAndView();
		//类别
		Map<String,Object> map =new  HashMap<String,Object>();
		map.put("typeId", 1);
		map.put("start",0);
		map.put("size", 8);
		List<Blog> blogList=blogService.listBlog(map);
		map.clear();
		map.put("typeId", 5);
		map.put("start",0);
		map.put("size", 8);
		List<Blog> happyList=blogService.listBlog(map);
		map.clear();
		map.put("typeId", 17);
		map.put("start",0);
		map.put("size", 8);
		List<Blog> wuraoList=blogService.listBlog(map);
		map.clear();
		map.put("typeId", 18);
		map.put("start",0);
		map.put("size", 8);
		List<Blog> breakList=blogService.listBlog(map);
		map.clear();
		map.put("typeId", 19);
		map.put("start",0);
		map.put("size", 8);
		List<Blog> stationLIst=blogService.listBlog(map);
		map.clear();


		List<Blog> topList=blogService.topCountList();
		mav.addObject("stationLIst",stationLIst);
		mav.addObject("breakList",breakList);
		mav.addObject("wuraoList",wuraoList);
		mav.addObject("blogList",blogList);
		mav.addObject("happyList",happyList);
		mav.setViewName("home");
		return mav;
	}

	/**
	 * 请求博客主页
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(@RequestParam(value="page",required=false)String page ,
			@RequestParam(value="typeId",required=false)String typeId,
			@RequestParam(value="releaseDateStr",required=false)String releaseDateStr,
			HttpServletRequest request)  {
		if(StringUtils.isBlank((String) request.getSession().getAttribute("color"))){
			request.getSession().setAttribute("color", "#E1FFFF");
		}
		int pageSize=12;
		int count=0;
		ModelAndView mav=new ModelAndView();
		Map<String,Object> map =new  HashMap<String,Object>();
		map.put("typeId", typeId);
		map.put("releaseDateStr", releaseDateStr);
		//计算出筛选的行数
		Long row=blogService.getTotal(map);
		//计算总页数
		count=(row.intValue() + pageSize - 1)/pageSize;
		if(StringUtil.isEmpty(page) || Integer.parseInt(page)<=0 ){
			page="1";
		}else if(Integer.parseInt(page)>=count){
			page=count+"";
		}

		PageBean pageBean =new PageBean(Integer.parseInt(page),pageSize);//分页
		map.put("start",pageBean.getStart() );
		map.put("size", pageBean.getPageSize());
		List<Blog> blogList=blogService.listBlog(map);
		for (Blog blog : blogList) {
			List<String> imageList=blog.getImageList();
			String blogInfo=blog.getContent();
			Document doc=Jsoup.parse(blogInfo);
			Elements jpgs=doc.select("img[src~=(?i)\\.(png|jpe?g)]");//指定为jpg,png,jpeg
			jpgs.attr("width","240").attr("height","150");//设置大小
			for (int i = 0; i < jpgs.size(); i++) {
				imageList.add(jpgs.get(i).toString());
				if(i==2) break;//只能显示3章缩略图
			}
		}

		StringBuffer param=new StringBuffer();
		if(!StringUtil.isEmpty(typeId)){//不为空追加到下一页
			param.append("typeId="+typeId+"&");
		}
		if(!StringUtil.isEmpty(releaseDateStr)){
			param.append("releaseDateStr="+releaseDateStr+"&");
		}

		mav.addObject("topBlog",blogService.listTopBlog(null));
		mav.addObject("page",page);
		mav.addObject("pageCode",PageUtil.genPagination(request.getContextPath()+"/index.html", blogService.getTotal(map), Integer.parseInt(page), pageSize, param.toString()));
		mav.addObject("blogList",blogList);
		mav.addObject("pageTitle","勿扰博客");
		mav.addObject("mainPage","foreground/blog/list.jsp");
		//跳转地址
		mav.setViewName("mainTemp");
		return mav;

	}


	/**
	 * App下载
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/download")
	public ModelAndView download()throws Exception{
		ModelAndView mav=new ModelAndView();
		mav.addObject("pageTitle", "App/Java_勿扰博客");
		mav.addObject("mainPage", "foreground/system/download.jsp");
		mav.setViewName("mainTemp");
		return mav;
	}


	@RequestMapping("/login")
	public ModelAndView login()throws Exception{
		ModelAndView mav=new ModelAndView();
		mav.setViewName("login");
		return mav;
	}
	
	
	/**
	 * 时光轴---测试
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/timeAxis")
	public ModelAndView timeAxis()throws Exception{
		ModelAndView mav=new ModelAndView();
		mav.addObject("pageTitle", "时光轴_勿扰博客");
		mav.addObject("mainPage", "foreground/system/timeAxis.jsp");
		mav.setViewName("mainTemp");
		return mav;
	}



	/**
	 * js在线聊天室
	 * @param model
	 * @return
	 */
	@RequestMapping("/qq")
	public String qq(ModelMap model){
		return "other/qq/index";
	}


	/**
	 * vip视频解析
	 * @param model
	 * @return
	 */
	@RequestMapping("/vip")
	public String vipView(ModelMap model){
		return "redirect:/other/vip/index.html";
	}


	/**
	 * music解析
	 * @param model
	 * @return
	 */
	@RequestMapping("/music")
	public String musicView(ModelMap model){
		return "redirect:http://music.junyuewl.com/?tdsourcetag=s_pcqq_aiomsg";
	}
}
